#! /usr/bin/env python
# coding=utf-8
import datetime

def getWeekBeginEndTuple(dt):
    ymd = datetime.datetime(year=dt.year, month=dt.month, day=dt.day)
    weekBegin = ymd + datetime.timedelta(days=-(dt.weekday()))
    weekEnd = ymd + datetime.timedelta(days=(7-dt.weekday()))
    
    return (weekBegin, weekEnd)

def getMonthBeginEndTuple(dt):
    monthBegin = datetime.datetime(year=dt.year, month=dt.month, day=1)
    if monthBegin.month == 12:
        monthEnd = datetime.datetime(year=dt.year + 1, month=1, day=1)
    else:
        monthEnd = datetime.datetime(year=dt.year, month=monthBegin.month + 1, day=1)

    return (monthBegin, monthEnd)

def getLast7BeginEndTuple(dt):
    ymd = datetime.datetime(year=dt.year, month=dt.month, day=dt.day)
    weekBegin = ymd + datetime.timedelta(days=-6)
    weekEnd = ymd + datetime.timedelta(days=1)
    
    return (weekBegin, weekEnd)

def getLast30BeginEndTuple(dt):
    ymd = datetime.datetime(year=dt.year, month=dt.month, day=dt.day)
    monthBegin = ymd + datetime.timedelta(days=-30)
    monthEnd = ymd + datetime.timedelta(days=1)

    return (monthBegin, monthEnd)
    
def getLastBeginEndTupleByDays(dt, days):
    ymd = datetime.datetime(year=dt.year, month=dt.month, day=dt.day)
    monthBegin = ymd + datetime.timedelta(days=-days)
    monthEnd = ymd + datetime.timedelta(days=1)

    return (monthBegin, monthEnd)

def getDateTimeExplain(calDateTime):
    now = datetime.datetime.now()
    dValue = now - calDateTime
    if dValue.days < 0:
        return calDateTime.strftime('%Y-%m-%d')
    if dValue.days > 2:
        return calDateTime.strftime('%Y-%m-%d')
    if dValue.days == 2:
        return '前天'
    if dValue.days == 1:
        return '昨天'
    if dValue.seconds >= 3600:
        return '%s小时前' % (str(dValue.seconds / 3600))
    if dValue.seconds < 3600 and dValue.seconds >= 1800:
        return '半小时前'
    seconds = dValue.seconds / 60
    if seconds == 0:
        return '1分钟前'
    return '%s分钟前' % str(seconds)

def getDateTimeYM(calDateTime):
    return calDateTime.strftime('%Y-%m')

def getDateTimeYMD(calDateTime):
    return calDateTime.strftime('%Y-%m-%d')

def getDateTimeYMDHM(calDateTime):
    return calDateTime.strftime('%Y-%m-%d %H:%M')

def getDateTimeYMDHMS(calDateTime):
    return calDateTime.strftime('%Y-%m-%d %H:%M:%S')

def getDateTimeYMDHMSF(calDateTime):
    return calDateTime.strftime('%Y_%m_%d_%H_%M_%S_%f')

def getYMDHMSFromString(dateTimeStr):
    return datetime.datetime.strptime(dateTimeStr, '%Y-%m-%d %H:%M:%S')

def getLastDateYMD(days=1):
    dt = datetime.datetime.now() + datetime.timedelta(days=-(days))
    return dt.strftime('%Y-%m-%d')
    
#最近一周,根据weeks,可以得到最近的几周的起始
def getLastWeekBeginEndTuple(dt, weeks=1):
    ymd = datetime.datetime(year=dt.year, month=dt.month, day=dt.day)
    weekBegin = ymd + datetime.timedelta(days=-(dt.weekday() + 7 * weeks))
    weekEnd = ymd + datetime.timedelta(days=-(dt.weekday() + 7 * (weeks - 1)))
    
    return (weekBegin, weekEnd)

def getLastWeekEnd(dt, weeks=1):
    ymd = datetime.datetime(year=dt.year, month=dt.month, day=dt.day)
    weekEnd = ymd + datetime.timedelta(days=-(dt.weekday() + 7 * (weeks - 1)))
    
    return weekEnd

def getLastBeginByMonths(dt, months):
    beginYear = dt.year
    beginMonth = dt.month - months
    if beginMonth <= 0:
        beginMonth += 12
        beginYear -= 1
    return datetime.datetime(year=beginYear, month=beginMonth, day=1)

#最近几个月的始末,末为前一个月最后一天
def getLastBeginEndTupleByMonths(dt, months):
    monthBegin = getLastBeginByMonths(dt, months)
    monthEnd = datetime.datetime(year=dt.year, month=dt.month, day=1)

    return (monthBegin, monthEnd)

#6个月前
def getLast6MonthBeforeEnd(dt):
    return getLastBeginByMonths(dt, 6)

#最近第几个月的起始,months必须<12
def getLastMonthBeginEndTuple(dt, months=1):
    monthBegin = getLastBeginByMonths(dt, months)
    monthEnd = getLastBeginByMonths(dt, months - 1)

    return (monthBegin, monthEnd)

def getLastMonthEnd(dt, months=1):
    return getLastBeginByMonths(dt, months - 1) + datetime.timedelta(seconds=-1)

def getMonthLi():
    result = {}
    #月份初始化
    monthLi = []
    for i in range(2011,2016):
        for j in range(1,13):
            tmpJ = j < 10 and '0' + str(j) or j
            monthLi.append('%s-%s' % (i, tmpJ))
    result['monthLi'] = monthLi
    
    current = datetime.datetime.now()
    preMonth = getLastBeginByMonths(current, 1)
    result['preMonth1'] = getDateTimeYM(preMonth)
    preMonth = getLastBeginByMonths(current, 2)
    result['preMonth2'] = getDateTimeYM(preMonth)
    return result
    
if __name__ == '__main__':
    dt = datetime.datetime(year=2010, month=1, day=1)
    print getLast6MonthBeforeEnd(dt)
    dt = datetime.datetime(year=2010, month=12, day=1)
    print getLast6MonthBeforeEnd(dt)

